<template>
  <div class="dialog">
    <el-dialog :title="title" :visible.sync="visible" :close-on-click-modal="false" width="70%" append-to-body>
      <el-form ref="tblMain" :model="tblMain" labelPosition="right" inline="" label-suffix=":" label-width="80px">
        <el-row :span="24">
          <el-col :span="7">
            <el-form-item label="客户名称">
              <el-input v-model="tblMain.param.name" size="small" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="手机号码">
              <el-input v-model="tblMain.param.phone" size="small" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" size="small" @click="initTableData()">查询</el-button>
            <el-button size="small" @click="dataReset()">清空</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table ref="tblMain" style="margin-top: 10px;" :data="tblMain.items" v-loading.body="tblMain.loading" border
        stripe>
        <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
        <el-table-column prop="name" label="客户名称" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="nickname" label="客户昵称" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="phone" label="手机号码" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="sex" label="性别" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.sex == '0'">男</span>
            <span v-else-if="scope.row.sex == '1'">女</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <el-button type="text" @click="handleSelect(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page.sync="tblMain.currentPage" :page-size="tblMain.pageSize" :total="tblMain.total"
        layout="total, sizes, prev, pager, next" @size-change="handleSizeChange"
        @current-change="handleCurrentChange"></el-pagination>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeNetWorker()">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  findCustomerListForStore
} from '@/api/index'
import tool from '@/tool'
export default {
  name: 'CustomerDialog',
  data() {
    return {
      title: "选择客户",
      visible: false,
      tblMain: tool.genTableData({
        param: {
          name: '',
          phone: ''
        },
        items: []
      }),
    }
  },
  mounted() {
  },
  methods: {
    select() {
      this.visible = true;
      this.initTableData();
    },
    handleSelect(row) {
      this.$emit('ok', row);
      this.visible = false;
    },
    closeNetWorker() {
      this.tblMain.items = [];
      this.visible = false;
    },
    handleSizeChange(val) {
      this.tblMain.pageSize = val
      this.initTableData()
    },
    handleCurrentChange(val) {
      this.tblMain.currentPage = val
      this.initTableData()
    },
    dataReset() {
      this.tblMain.currentPage = 1
      this.tblMain.pageSize = 10
      this.tblMain.param.name = ''
      this.tblMain.param.phone = ''
      this.initTableData()
    },
    initTableData() {
      let me = this
      let param = {
        name: me.tblMain.param.name,
        phone: me.tblMain.param.phone,
        page: me.tblMain.currentPage,
        size: me.tblMain.pageSize
      }
      me.tblMain.loading = true
      findCustomerListForStore(param).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.tblMain.items = data.data.record
          me.tblMain.total = data.data.total
        }
      })
    },
  }
}
</script>

<style scoped></style>
